package net.onething.dc.hdfs2bos;

import java.io.IOException;

import org.apache.hadoop.fs.*;

public class CommonHdfsUtils {

    public static Path convertTobosPath(ConfigReader configReader,
            Path hdfsFilePath) throws IOException {
        if (null == hdfsFilePath) {
            throw new NullPointerException("hdfs file path is null");
        }
        String srcPath =
                new Path(configReader.getSrcHdfsPath()).toUri().getPath();
        String hdfsFolderPath = srcPath;
        if (configReader.getHdfsFs().getFileStatus(new Path(srcPath)).isFile()) {
            hdfsFolderPath =
                    srcPath.substring(0, srcPath.lastIndexOf("/")) + "/";
        }
        String filePath = hdfsFilePath.toUri().getPath();           // 文件的实际路径
        String destPath = configReader.getDestBosPath();            // BOS上的目的路径
        if (!destPath.endsWith("/")) {
            destPath = destPath + "/";
        }

        String bosPath;
        if (configReader.getHdfsFs().getFileStatus(new Path(filePath)).isFile()) {
            bosPath = filePath.replaceFirst(hdfsFolderPath, destPath);
        } else {
            bosPath = filePath.replaceFirst(hdfsFolderPath, destPath) + "/";
        }

        return new Path(Utils.trimDoubleSlash(bosPath));
    }
}
